﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Demos.Threads.Services;

namespace Demos.Threads
{
    /// <summary>
    /// 多线程
    /// </summary>
    public class DoubleThread
    {
        /// <summary>
        /// 运行多个线程
        /// </summary>
        public static void Start()
        {
            //创建线程
            Thread thread = new Thread(new ThreadStart(Test1));
            //运行线程
            thread.Start();

            //创建第二个线程
            Thread thread2 = new Thread(new ThreadStart(Test1));
            //运行第二个线程
            thread2.Start();
        }

        public static void Start2()
        {
            var result = Task.Run(() =>
                Test2("查询用户数据...")).Result;
            System.Console.WriteLine($"任务调用返回={result}");
        }

        /// <summary>
        /// 并行执行任务
        /// </summary>
        public static void Start3()
        {
            //创建多个任务
            List<Action> tasks = new List<Action>();
            tasks.Add(() => Test3("任务1运行参数..."));
            tasks.Add(() => Test3("任务2运行参数..."));
            tasks.Add(() => Test3("任务3运行参数..."));

            //并行运行
            Parallel.ForEach(tasks, ts => {
                ts.Invoke();
            });

            //结束运行
            System.Console.WriteLine(" 并行任务执行结束...");
        }

        /// <summary>
        /// 并行运行-指定CPU数量
        /// </summary>
        public static void Start4()
        {
            //创建多个任务
            List<Action> tasks = new List<Action>();
            tasks.Add(() => Test3("任务1运行参数..."));
            tasks.Add(() => Test3("任务2运行参数..."));
            tasks.Add(() => Test3("任务3运行参数..."));
            tasks.Add(() => Test3("任务4运行参数..."));
            tasks.Add(() => Test3("任务5运行参数..."));
            tasks.Add(() => Test3("任务6运行参数..."));

            //并行运行
            ParallelOptions parallelOptions = new ParallelOptions();
            //指定CPU核心数为2
            parallelOptions.MaxDegreeOfParallelism = 2;
            Parallel.ForEach(tasks, parallelOptions, ts => {
                ts.Invoke();
            });
            //结束运行
            System.Console.WriteLine(" 并行任务执行结束...");
        }


        /// <summary>
        /// 多线程并发执行
        /// </summary>
        public static void Start5()
        {
            //创建多个任务
            var tasks = new List<Task>
                {
                    Test4("任务1线程参数"),
                    Test4("任务2线程参数"),
                    Test4("任务3线程参数")
                };
            //并行运行
            Task.WhenAll(tasks).Wait();
            //结束运行
            System.Console.WriteLine(" 并行任务执行结束...");
        }


        static void Test1()
        {
            var user = new UserService().GetUserByName("zhangsan1");
            System.Console.WriteLine($"查询用户信息返回：{JsonSerializer.Serialize(user)}");
            System.Console.WriteLine("---------------------------------------------------------------");
        }

        /// <summary>
        /// 带参数带返回
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        static string Test2(object param)
        {
            System.Console.WriteLine($"线程参数={param}");
            UserService userService = new UserService();

            Guid id = Guid.Parse("817ec66f-8afe-4c98-a71f-426c39d830d9");
            var user = userService.GetUser(id);
            if (user != null)
                return JsonSerializer.Serialize(user);
            else
                return "没有数据";
        }

        /// <summary>
        /// 带参数带返回
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        static string Test3(object param)
        {
            System.Console.WriteLine($"线程参数={param}");
            UserService userService = new UserService();

            Guid id = Guid.Parse("817ec66f-8afe-4c98-a71f-426c39d830d9");
            System.Console.WriteLine($"查询用户{id}数据");
            var user = userService.GetUser(id);
            if (user != null)
                return JsonSerializer.Serialize(user);
            else
                return "没有数据";
        }

        static async Task Test4( object param)
        {
            System.Console.WriteLine($"请求参数：{param}");
            var user = new UserService().GetUserByName("zhangsan1");
            System.Console.WriteLine($"查询用户信息返回：{JsonSerializer.Serialize(user)}");
            System.Console.WriteLine("---------------------------------------------------------------");
        }
    }
}
